import { useEffect } from 'react';
import { withRouterContext } from '@/context/router/routerContext';

const GlobalEventListener = (props) => {

    const { navigate } = props;

    useEffect(() => {

        const handleNavigate = (event) => {
            const { path } = event.detail;
            navigate(path);
        };
        window.addEventListener('navigateTo', handleNavigate);

        return () => {
            window.removeEventListener('navigateTo', handleNavigate);
        };
    }, [navigate]);
    
    return null;
};

export default withRouterContext(GlobalEventListener);